<template>
  <div class="user-wrap">
      <el-table
        :data="tableData"
        style="width: 100%">
        <el-table-column
            label="姓名"
            prop="name">
        </el-table-column>
        <el-table-column
            label="邮箱"
            prop="email">
        </el-table-column>
        <el-table-column
            label="年龄"
            prop="age">
        </el-table-column>
        <el-table-column
            label="性别"
            prop="gender">
        </el-table-column>
        <el-table-column
            label="描述"
            prop="desc">
        </el-table-column>
        <el-table-column
            align="right">
            <template #default="scope">
                <el-button
                size="mini"
                @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                <!-- <el-button
                size="mini"
                type="danger"
                @click="handleDelete(scope.$index, scope.row)">删除</el-button> -->
            </template>
        </el-table-column>
    </el-table>
    <el-dialog title="个人信息" v-model="dialogFormVisible">
        <el-form :model="form">
            <el-form-item label="姓名" :label-width="formLabelWidth">
                <el-input v-model="form.name" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="邮箱" :label-width="formLabelWidth">
                <el-input v-model="form.email" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="年龄" :label-width="formLabelWidth">
                <el-input v-model.number="form.age" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="性别" :label-width="formLabelWidth">
                <el-input v-model="form.gender" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="描述" :label-width="formLabelWidth">
                <el-input v-model="form.desc" autocomplete="off"></el-input>
            </el-form-item>
        </el-form>
        <template #footer>
            <span class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="confirmUser">确 定</el-button>
            </span>
        </template>
    </el-dialog>
  </div>
</template>

<script>
import { ref} from 'vue'
import api from '../../../api/index'
import { ElMessage } from 'element-plus'
export default {
    setup() {
        const dialogFormVisible = ref(false)
        const formLabelWidth = ref('120px')
        const userId = ref(0)
        // 获取资源列表
        const tableData = ref([])
        async function getMyInfo () {
            const res = await api.getMyInfo()
            if (res.data.code === 200) {
                tableData.value = [res.data.data]
            } else {
                ElMessage.error('获取个人信息失败：' + res.data.msg)
            }
        }
        getMyInfo()

        const form = ref({
            name: '',
            email: '',
            age: '',
            gender: '',
            desc: '',
        })

        // 提交个人信息
        async function confirmUser () {
        // 验证信息是否齐全
        if (!(form.value.name)) {
            ElMessage.error('表单信息不全')
            return
        }
            updateUser(form.value)
        }

        // 编辑团队
        async function handleEdit (index, row) {
        dialogFormVisible.value = true
        form.value.name = row.name
        form.value.email = row.email
        form.value.age = row.age
        form.value.gender = row.gender
        form.value.desc = row.desc
        userId.value = row.id
        }
        // 修改团队信息
        async function updateUser (params) {
            const res = await api.updateUser(userId.value, params)
            if (res.data.code === 200) {
                ElMessage.success({
                    message: '修改成功',
                    type: 'success'
                })
                getMyInfo()
                dialogFormVisible.value = false
            } else {
                ElMessage.error('修改失败：' + res.data.msg)
            }
        }
        return {
            dialogFormVisible,
            formLabelWidth,
            tableData,
            form,
            confirmUser,
            handleEdit,
        }
    }
    
}
</script>

<style lang="scss" scoped>
    .user-wrap{
        width: 100%;
        min-width: 500px;
    }
</style>